package Tests;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.locks.ReentrantLock;

import Algorthim.Algorithim;
import Algorthim.HeavyWeight;
import Algorthim.QuqueBreadthFirst;
import Algorthim.SingleThreaded;
import Algorthim.Splits;

import com.rits.cloning.Cloner;

import phylogenetictree.PhylogeneticTree;
import searchTree.searchTreeNode;



public class tests {
	private static Cloner cloner = new Cloner();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
			Algorithim program = new SingleThreaded();
			Algorithim programTwo = new HeavyWeight(null, true, new ReentrantLock(), new Splits(4), new Vector<Thread>());
			
			for(int size = 50;size < 200;size += 25)
			{
				for(int i = 5; i < 12;i++)
				{
					runTest("First ", size, 3, i, i+1,program,programTwo);
					runTest("Secound ", size, 3, i, i+1,program,programTwo);
				}
			}
	}

	private static void runTest(String identifyer, int size,int tbrs,int runOption,int k,Algorithim program, Algorithim programTwo)
	{
		String resultofTest = "";
		long first = 0;
		long secound = 0;
		
		try
		{
			PhylogeneticTree tree = new PhylogeneticTree(size);
			PhylogeneticTree mutant = tree.createMutant(tbrs);
			PhylogeneticTree treeTwo = cloner.deepClone(tree);
			PhylogeneticTree mutantTwo = cloner.deepClone(mutant);
//			Algorithim program = new SingleThreaded();
//			Algorithim programTwo = new HeavyWeight(null, true, new ReentrantLock(), new Splits(4), new Vector<Thread>());
			
			long startTime = System.currentTimeMillis();
			ArrayList<searchTreeNode> result = program.solve(tree, mutant, runOption, k);
			Collections.sort(result);
			first = System.currentTimeMillis() - startTime;
			
			resultofTest += "Number of cuts " + result.get(0).getNumberOfCuts() + "\n";
			resultofTest += result.get(0).getTree().toCommaSeperatedFile() + "\n";
			
			startTime = System.currentTimeMillis();
			ArrayList<searchTreeNode> resultTwo = programTwo.solve(treeTwo, mutantTwo, runOption, k);
			Collections.sort(resultTwo);
			resultofTest += "Number of cuts " + result.get(0).getNumberOfCuts() + "\n";
			resultofTest += resultTwo.get(0).getTree().toCommaSeperatedFile() + "\n";
			secound = System.currentTimeMillis() - startTime;
		}
		catch (Exception e)
		{
			resultofTest = "Fail";
		}
		
		File newFile = new File("results/" + identifyer + " Size " + size + " tbrs " + tbrs + " run Option " + runOption + " number of cuts " + k + " program One " + program + " program Two " + programTwo);
		resultofTest += "\n" + "single thread time " + first / 1000 + " multi " + secound / 1000;
		writeFile(resultofTest, newFile);
	}
	
	private static void writeFile(String serilizedTree,File file)
	{
		FileWriter fstream;
		try 
		{
			fstream = new FileWriter(file);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(serilizedTree);
			out.close();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
